
package com.hlkj.pay.service.order.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hlkj.pay.common.constants.RedisConstants;
import com.hlkj.pay.dto.order.RefundOrderQueryDto;
import com.hlkj.pay.infrastructure.mapper.order.RefundOrderMapper;
import com.hlkj.pay.infrastructure.model.order.RefundOrderDO;
import com.hlkj.pay.service.CommonSnFilterService;
import com.hlkj.pay.service.order.IRefundOrderService;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;

import java.util.List;

/**
 * @author HlpayTeam
 * @date 2024/10/12 10:19
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class RefundOrderService extends CommonSnFilterService implements IRefundOrderService {

    private final RefundOrderMapper refundOrderMapper;

    @Override
    public void addRefundOrder(RefundOrderDO payOrderDO) {
        refundOrderMapper.insert(payOrderDO);
    }

    @Override
    @Cacheable(value = RedisConstants.PAY_REFUND_INFO, key = "#refundOrderNo", unless = "#result == null")
    public RefundOrderDO queryRefundOrder(String refundOrderNo) {
        return refundOrderMapper.selectOne(RefundOrderDO::getRefundOrderNo, refundOrderNo);
    }

    @Override
    @Cacheable(value = RedisConstants.PAY_REFUND_INFO_SN, key = "#sn+'_'+#mchRefundOrderNo", unless = "#result == null")
    public RefundOrderDO queryRefundOrderByMerOrderNo(String sn, String mchRefundOrderNo) {
        return refundOrderMapper.selectOne(RefundOrderDO::getSn, sn, RefundOrderDO::getMchRefundOrderNo, mchRefundOrderNo);
    }

    @Override
    @Caching(evict = { @CacheEvict(value = RedisConstants.PAY_REFUND_INFO, key = "#refundOrderNo"),
            @CacheEvict(value = RedisConstants.PAY_REFUND_INFO_SN, key = "#sn+'_'+#mchRefundOrderNo"), })
    public void updateRefundOrder(RefundOrderDO updateRefundOrderDO, String refundOrderNo, String sn, String mchRefundOrderNo) {
        refundOrderMapper.updateById(updateRefundOrderDO);
    }

    @Override
    public RefundOrderDO queryRefundLastOne(String payOrderNo) {
        RefundOrderQueryDto merchantInfoQueryDO = new RefundOrderQueryDto();
        merchantInfoQueryDO.setPayOrderNo(payOrderNo);
        LambdaQueryWrapper<RefundOrderDO> lambdaQueryWrapper = buildWrapper(merchantInfoQueryDO);
        lambdaQueryWrapper.orderByDesc(RefundOrderDO::getId);
        lambdaQueryWrapper.last("limit 1");
        return refundOrderMapper.selectOne(lambdaQueryWrapper);
    }

    @Override
    public PageResult<RefundOrderDO> queryPage(RefundOrderQueryDto refundOrderQueryDto) {
        return refundOrderMapper.selectPage(refundOrderQueryDto, buildWrapper(refundOrderQueryDto));
    }

   
    @Override
    public Integer orderCount(RefundOrderQueryDto refundOrderQueryDto) {
        return refundOrderMapper.selectCount(buildWrapper(refundOrderQueryDto)).intValue();
    }

    @Override
    public List<RefundOrderDO> queryList(RefundOrderQueryDto refundOrderQueryDto) {
        return refundOrderMapper.selectList(buildWrapper(refundOrderQueryDto));
    }

    /**
     * 查询条件封装
     *
     * @param refundOrderQueryDto
     * @return
     */
    LambdaQueryWrapper<RefundOrderDO> buildWrapper(RefundOrderQueryDto refundOrderQueryDto) {
        filterSn(refundOrderQueryDto);
        LambdaQueryWrapperX<RefundOrderDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(RefundOrderDO::getPayOrderNo, refundOrderQueryDto.getPayOrderNo());
        queryWrapper.eqIfPresent(RefundOrderDO::getMchOrderNo, refundOrderQueryDto.getMchOrderNo());
        queryWrapper.eqIfPresent(RefundOrderDO::getMchRefundOrderNo, refundOrderQueryDto.getMchRefundOrderNo());
        queryWrapper.eqIfPresent(RefundOrderDO::getRefundOrderNo, refundOrderQueryDto.getRefundOrderNo());
        queryWrapper.eqIfPresent(RefundOrderDO::getRefundStatus, refundOrderQueryDto.getRefundStatus());
        queryWrapper.eqIfPresent(RefundOrderDO::getNotifyState, refundOrderQueryDto.getNotifyState());
        queryWrapper.eqIfPresent(RefundOrderDO::getSn, refundOrderQueryDto.getSn());
        queryWrapper.inIfPresent(RefundOrderDO::getSn, refundOrderQueryDto.getSnList());
        queryWrapper.geIfPresent(RefundOrderDO::getCreateTime, refundOrderQueryDto.getStartQueryTime());
        queryWrapper.leIfPresent(RefundOrderDO::getCreateTime, refundOrderQueryDto.getEndQueryTime());
        queryWrapper.likeIfPresent(RefundOrderDO::getAppId, refundOrderQueryDto.getAppId());
        return queryWrapper;
    }
}
